
#ifndef _TIMER_H
#define _TIMER_H

#include <list>
#include <windows.h>
#include <time.h>

typedef struct
{
	time_t time;
	long id;
	void* data;
} TIMEENTRY;

typedef std::list<TIMEENTRY>::iterator	ItLE;


class Timer
{
public:
protected:
private:
	std::list<TIMEENTRY>	m_list;
	time_t					m_time;
	HANDLE					m_htimer;
	long					m_id;
	long					m_tid;
	bool					m_done;
	CRITICAL_SECTION		m_mutex;

public:
	Timer ();
	~Timer ();

	HANDLE GetHandle (void);
	long AddDelta (long time, void* data);
	long Add (time_t time, void* data);
	bool Remove (void* who);
	void ClearAll (void);
	long TimerExpired (void*& data);
	void TimerCompletion (void);
	void lock();
	void unlock();

protected:
	void StopTimer();
	long AddToList (ItLE start, TIMEENTRY te);
	ItLE FindId (long id);
	ItLE FindClosest (time_t time);
	time_t GetTime (void);
	BOOL SetTime (time_t time);

private:
	long GetId (void);

};


#endif
